package com.zw.april14thh.vo;

import lombok.Data;

import java.io.Serializable;

@Data
public class AjaxResult implements Serializable {
    //     状态码
    private int code;
    //     提示
    private String msg;
    //     数据
    private Object data;

    public <T> T getData() {
        return (T) data;
    }
    public <T> T getData(Class<T> clazz) {
        return clazz.cast(data);
    }

    public static <T> AjaxResult success(String msg, T data) {
        AjaxResult res = new AjaxResult();
        res.setMsg(msg);
        res.setCode(0);
        res.setData(data);
        return res;
    }

    public static <T> AjaxResult success(int code, T data) {
        AjaxResult res = new AjaxResult();
        res.setMsg("操作成功");
        res.setCode(code);
        res.setData(data);
        return res;
    }

    public static <T> AjaxResult success(int code, String msg, T data) {
        AjaxResult res = new AjaxResult();
        res.setMsg(msg);
        res.setCode(code);
        res.setData(data);
        return res;
    }

    public static <T> AjaxResult success(T data) {
        AjaxResult res = new AjaxResult();
        res.setMsg("操作成功！");
        res.setCode(0);
        res.setData(data);
        return res;
    }

    public static AjaxResult success(String msg) {
        AjaxResult res = new AjaxResult();
        res.setMsg(msg);
        res.setCode(0);
        return res;
    }

    public static AjaxResult success() {
        AjaxResult res = new AjaxResult();
        res.setMsg("操作成功！");
        res.setCode(0);
        return res;
    }

    public static AjaxResult error(int code, String msg) {
        AjaxResult res = new AjaxResult();
        res.setMsg(msg);
        res.setCode(code);
        return res;
    }

    public static AjaxResult error(int code) {
        AjaxResult res = new AjaxResult();
        res.setMsg("操作失败！");
        res.setCode(code);
        return res;
    }

    public static AjaxResult error() {
        AjaxResult res = new AjaxResult();
        res.setMsg("操作失败！");
        res.setCode(1);
        return res;
    }

    public static AjaxResult error(String msg) {
        AjaxResult res = new AjaxResult();
        res.setMsg(msg);
        res.setCode(1);
        return res;
    }

    public static AjaxResult response(int res) {
        return res > 0 ? success() : error();
    }
}
